package echat.future.cn.echat.video;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import echat.future.cn.echat.R;
import echat.future.cn.echat.my.MyFragment;
import echat.future.cn.echat.my.bean.UserBean;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.video.domain.VideoData;
import lumingweihua.future.cn.baselibgxh.base.BaseFragment;
import lumingweihua.future.cn.baselibgxh.base.MyApplication;
import lumingweihua.future.cn.baselibgxh.network.AbstractRequest;
import lumingweihua.future.cn.baselibgxh.network.BeanRequest;
import lumingweihua.future.cn.baselibgxh.network.NetworkManager;
import lumingweihua.future.cn.baselibgxh.network.PureListRequest;
import lumingweihua.future.cn.baselibgxh.network.ReqTag;
import lumingweihua.future.cn.baselibgxh.network.entity.MamaHaoServerError;

/**
 * Created by Administrator on 2018/8/16.
 */

public class VideoParentFragment extends BaseFragment {
    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_video_parent, container, false);
    }

    private FocusFragment[] fragmentPages;
    private TextView[] tv_tabs;
//    private VideoFragment videoFragment1;
//    private VideoFragment videoFragment2;
    @Override
    protected void initData(Bundle savedInstanceState) {
//        videoFragment1 = new VideoFragment();
//        videoFragment2 = new VideoFragment();
//        videoFragment2.isCity = true;

        fragmentPages = new FocusFragment[]{new FocusFragment(), new FocusFragment(), new FocusFragment()};
        fragmentPages[1].isNew = true;
        fragmentPages[2].isFocus = true;
        //底部tab
        tv_tabs = new TextView[]{findTextView(R.id.tv_tuijian),
                findTextView(R.id.tv_tongcheng),
                findTextView(R.id.tv_guanzhu)};

        for (TextView tv : tv_tabs)
            tv.setOnClickListener(this);

        // 添加显示第一个fragment
        getChildFragmentManager().beginTransaction()
                .add(R.id.fragment_container, fragmentPages[0]).commit();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private int currentTabIndex;
    private boolean hasPlay1;
    private boolean hasPlay2;
    /**
     * 切换tab
     *
     * @param index
     */
    @SuppressWarnings("deprecation")
    public void changeTab(int index) {
        //当前tab不执行操作
        if (currentTabIndex == index)
            return;

        pause();

        //切换之前选中tab的图标和文字颜色
        tv_tabs[currentTabIndex].setTextColor(Color.parseColor("#C9C9C9"));
        tv_tabs[currentTabIndex].setTextSize(15);
        //切换当前选中tab的图标和文字颜色
        tv_tabs[index].setTextColor(Color.WHITE);
        tv_tabs[currentTabIndex].setTextSize(16);

        FragmentTransaction ft = getChildFragmentManager()
                .beginTransaction();
        ft.hide(fragmentPages[currentTabIndex]);
        if (!fragmentPages[index].isAdded()) {
            ft.add(R.id.fragment_container, fragmentPages[index]);
        }
        ft.show(fragmentPages[index]).commit();

//        switch (index) {
//            case 0:
//                if(currentTabIndex == 1)
//                    videoFragment1.resume();
//                else if(hasPlay2){
//                    videoFragment1.resume();
//                    hasPlay2 = false;
//                }else
//                    videoFragment1.restart();
//                hasPlay1 = true;
//                break;
//            case 1:
//                if(currentTabIndex == 0)
//                    videoFragment2.resume();
//                else if(hasPlay1){
//                    videoFragment2.resume();
//                    hasPlay1 = false;
//                }
//                else
//                    videoFragment2.restart();
//                hasPlay2 = true;
//                break;
//
//            default:
//                break;
//        }

        currentTabIndex = index;
    }

    public void pause(){
//        if(currentTabIndex == 0 && videoFragment1!=null)
//            videoFragment1.pause();
//        else if(currentTabIndex == 1 && videoFragment2!=null)
//            videoFragment2.pause();
    }

    @Override
    protected void processClick(View v) {
        switch (v.getId()) {
            case R.id.tv_tuijian:
                changeTab(0);
                break;
            case R.id.tv_tongcheng:
                changeTab(1);
                break;
            case R.id.tv_guanzhu:
                changeTab(2);
                break;

            default:
                break;
        }
    }
}
